﻿using ORM.Applicaiton;
using ORM.Base;
using ORM.Model;
using ORM.Parser.Design;
using ORM.Parser.Design.Format;
using ORM.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Test.Modal;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            DBUtil.Init();
            DBUtil dbutil = new DBUtil();

            Console.WriteLine("主线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());

            #region 增删改查
            //User user = new User();
            //user.UserGuid = "yanpeng19940119";
            //user.Name = "yanpeng";
            //user.LoginName = "15195007397";
            //user.PassWord = "123456";
            //user.Age = 26;
            //user.AddDate = DateTime.Now;

            ////增
            //dbutil.Insert(user);

            //删
            //dbutil.Remove(user);

            //改
            //user.Age = 29;
            //dbutil.Save(user);

            //查
            //User user = dbutil.FindOne<User>("yanpeng19940119");
            //Console.WriteLine(user.LoginName);
            #endregion

            #region 通过委托执行事物
            string ExceptionSTr = string.Empty;
            DBUtil dbutil2 = new DBUtil();
            bool issuccess = dbutil.ExecuteTransition_SDB(delegate()
            {
                User user = new User();
                user.UserGuid = "ceshi00992";
                user.Name = "yanpeng";
                user.LoginName = "15195007397";
                user.PassWord = "123456";
                user.Age = 26;
                user.AddDate = DateTime.Now;
                dbutil.Insert(user);
                dbutil.ExecuteSQL("update User set Age=27");
                throw new Exception("111");
                dbutil2.ExecuteSQL("update User set Age=33");

            }, out ExceptionSTr);
            Console.WriteLine(issuccess);
            Console.WriteLine(ExceptionSTr);
            #endregion

            #region 测试Emit效率
            //User user = new User();
            //DateTime d1 = DateTime.Now;
            //for (int i = 0; i < 1000000; i++)
            //{
            //    user.AddDate = DateTime.Now;
            //}
            //DateTime d2 = DateTime.Now;
            //Console.WriteLine("直接赋值耗时：" + (d2 - d1).TotalMilliseconds + "毫秒");

            //var type = typeof(User);
            //MapperEntity entity = MapperBuilder.GetInstance().GetMap(type);
            //var setter = entity.EmitDic["AddDate"].EmitSetter;
            //d1 = DateTime.Now;
            //for (int i = 0; i < 1000000; i++)
            //{
            //    setter(user, DateTime.Now);
            //}
            //d2 = DateTime.Now;
            //Console.WriteLine("直接赋值耗时：" + (d2 - d1).TotalMilliseconds + "毫秒");
            #endregion

            #region 智能保存
            //Console.WriteLine("查询");
            //User user = dbutil.FindOne<User>("yanpeng19940119");
            //Console.WriteLine("全字段保存");
            //user.LoginName = "asdasdas";
            //dbutil.Save(user);
            //Console.WriteLine("智能保存");
            //user.LoginName = "112233";
            //dbutil.SaveForTrace(user);
            #endregion

            #region 基类BaseEnity字段json序列化时屏蔽序列化测试
            //User user = dbutil.FindOne<User>("yanpeng19940119");
            //string jsonstr = JsonConvert.SerializeObject(user);
            //Console.WriteLine(jsonstr);
            #endregion

            #region 弱引用测试
            //DBRow row = new DBRow("User", "UserGuid", "6cc5b098-0966-4b21-b93e-bdd25fded3ea");
            //row.TraceSQL = true;
            //row["Name"] = "122yan";
            //row["LoginName"] = "heheheh";
            //row["AddDate"] = DateTime.Now;
            //row.Insert();

            //row["Age"] = 33;
            //row.Update();
            //row.Delete();
            #endregion

            #region 弱引用事物
            //string exceptionstr = string.Empty;
            //bool issuccess = DBRow.ExecuteTransition_SDB(delegate(DbTransaction Transition)
            //{

            //    DBRow row = new DBRow("User", "UserGuid", "6cc5b098-0966-4b21-b93e-bdd25fded3ea");
            //    row["Name"] = "122yan";
            //    row["LoginName"] = "heheheh";
            //    row["AddDate"] = DateTime.Now;
            //    row.Transition = Transition;
            //    row.Insert();

            //    DBRow row1 = new DBRow("User", "UserGuid", "11223344");
            //    row1.Transition = Transition;
            //    row1.Delete();
            //    throw new Exception("1111");

            //}, out exceptionstr);
            //Console.WriteLine(issuccess);

            #endregion

            #region 测试SQL语义解析
            //InsertStatement insertstatement = new InsertStatement();
            //insertstatement.ConnectStringName = "Base_ConnectionString";
            //string insertsql = "insert into user (userguid,name,age) values ('111','2222','21')";
            //Console.WriteLine(FormatStyle.Basic.Formatter.Format(insertsql));
            //insertstatement.PKName = "userguid";
            //insertstatement.Parse(insertsql);
            //Console.WriteLine(insertstatement.GetRollBackSQL());

            //UpdateStatement updatestatement = new UpdateStatement();
            //updatestatement.ConnectStringName = "Base_ConnectionString";
            //string updatesql = "update user set age='15',name='asadasda' where userguid='yanpeng19940119'";
            //Console.WriteLine(FormatStyle.Basic.Formatter.Format(updatesql));
            //updatestatement.Parse(updatesql);
            //Console.WriteLine(updatestatement.GetRollBackSQL());

            //DeleteStatement deletestatement = new DeleteStatement();
            //deletestatement.ConnectStringName = "Base_ConnectionString";
            //string deletesql = "delete from user where userguid='yanpeng19940119'";
            //Console.WriteLine(FormatStyle.Basic.Formatter.Format(deletesql));
            //deletestatement.Parse(deletesql);
            //Console.WriteLine(deletestatement.GetRollBackSQL());
            #endregion

            #region 测试自动的补偿式TCC事物
            //string exceptionstr = string.Empty;
            //User user = new User();
            //user.UserGuid = "ceshi00992";
            //user.Name = "yanpeng";
            //user.LoginName = "15195007397";
            //user.PassWord = "123456";
            //user.Age = 26;
            //user.AddDate = DateTime.Now;
            //User user1 = dbutil.FindOne<User>("yanpeng19940119");
            //var orginalage = user1.Age;
            //bool issuccess = dbutil.ExecuteTransition_DDB_AutoRollBack(delegate()
            //{
            //    dbutil.Insert(user, "Test_ConnectionString");
            //    user1.Age = 44;
            //    dbutil.Save(user1);
            //    throw new Exception("111111");

            //},  out exceptionstr);
            //Console.WriteLine(issuccess);
            #endregion

            Console.WriteLine("执行完毕");
            Console.ReadKey();
        }
    }
}
